package com.changhongit.config.swagger;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Configuration {
    /**
     * 后台接口文档
     */

    //api接口包扫描路径
    public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.changhongit.controller";
    public static final String VERSION = "1.0.0";
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档，忽略哪些请求
                .build()
                //安全设置
                .securitySchemes(securitySchemes())
                ;

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("腾讯会议管理系统后台API") //设置文档的标题
                .description("腾讯会议管理系统 API 接口文档") // 设置文档的描述
                .version(VERSION) // 设置文档的版本信息-> 1.0.0 Version information
                .termsOfServiceUrl("http://cpaas.changhongit.com") // 设置文档的License信息->1.3 License information
                .build();
    }

    //swagger安全配置信息
    private List<ApiKey> securitySchemes(){
        List<ApiKey> list=new ArrayList<>();
        ApiKey apiKey=new ApiKey("Authorization","Authorization","Header");
        list.add(apiKey);
        return list;
    }

    /**
     * 配置security对swagger的权限
     * @return
     */
    private List<SecurityContext> securityContext(){
        List<SecurityContext> list=new ArrayList<>();
        list.add(getSecurityContext());
        return list;
    }

    /**
     * 授权路径
     * @return
     */
    private SecurityContext getSecurityContext(){
        return SecurityContext.builder()
                .securityReferences(securityReference())
                .forPaths(PathSelectors.regex("hello/.*"))
                .build();
    }

    /**
     * 给swagger授权，可以进行接口测试
     * @return
     */
    private List<SecurityReference> securityReference(){
        List<SecurityReference> list=new ArrayList<>();
        //授权范围，全局
        AuthorizationScope scope=new AuthorizationScope("global","accessEverything");
        AuthorizationScope[] scopes = new AuthorizationScope[1];
        scopes[0]=scope;
        list.add(new SecurityReference("Authorization",scopes));
        return list;
    }



}
